package housing.task2.deserializator;

import housing.task1.actor.Address;
import housing.task1.actor.Renter;
import housing.task2.factory.JSNFactory;
import housing.task2.parser.AbstractParser;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Linderhof
 * Date: 30.11.2010
 * Time: 20:45:06
 * To change this template use File | Settings | File Templates.
 */
public class JSNReader extends AbstractReader {
    JSNFactory factory = null;
    AbstractParser parser = null;

    public JSNReader() {
        factory = new JSNFactory();
        parser = factory.createParser();
    }

    /**
     * FIXME: Must throw custom exceptions!!!
     *
     * @param file
     * @return
     */
    @Override
    public List<Renter> readRenter(File file) {
        List<Renter> renters =
                new ArrayList<Renter>();

        List<String[]> objectsFieldValues = parser.parse(file);

        for (String[] line : objectsFieldValues) {
            String id = line[0];
            String firstName = line[1];
            String lastName = line[2];
            String address_id = line[3];
            String street = line[4];
            String house = line[5];
            String flat = line[6];

            Renter renter;
            Address address;

            address = new Address(new Long(address_id),
                    street,
                    new Integer(house),
                    new Integer(flat)
            );

            renter = new Renter(new Long(id),
                    firstName,
                    lastName,
                    address
            );

            renters.add(renter);
        }

        return renters;
    }

    /**
     * Must throw custom exceptions!!!
     *
     * @param file
     * @return
     */
    @Override
    public List<Address> readAddress(File file) {
        List<Address> addresses = new ArrayList<Address>();

        List<String[]> objectsFieldValues = parser.parse(file);

        for (String[] line : objectsFieldValues) {
            String id = line[0];
            String street = line[1];
            String house = line[2];
            String flat = line[3];

            addresses.add(new Address(new Long(id),
                    street,
                    new Integer(house),
                    new Integer(flat)
            )
            );

        }
        return addresses;
    }
}
